#pragma once

#include"RBTree.h"

template<class K, class V>
class MyMap
{
public:
	struct MapKeyOfT
	{
		const K& operator()(pair<K, V>& T)
		{
			return T.first;
		}
	};
	typedef typename RBTree<K, pair<K, V>, MapKeyOfT>::Iterator Iterator;
	typedef typename RBTree<K, pair<K, V>, MapKeyOfT>::const_Iterator const_Iterator;

	//MyMap()
	//	: _tree(nullptr)
	//{}

	//MyMap(const MyMap<K, V>& tree)
	//{
	//	_tree(tree._tree);
	//}

	pair<Iterator, bool> Insert(const pair<K, V> p)
	{
		return _tree.Insert(p);
	}

	Iterator begin()
	{
		return _tree.begin();
	}

	Iterator end()
	{
		return _tree.end();
	}

	Iterator find(K key)
	{
		return _tree.Find(key);
	}

	V& operator[](const K& key)
	{
		pair<Iterator, bool> ret = _tree.Insert(make_pair(key, V()));
		return (ret.first)->second;
	}

private:
	RBTree<K, pair<K, V>, MapKeyOfT> _tree;
};

void test_map()
{
	MyMap<string, string> dict;
	dict.Insert(make_pair("sort", "齬唗"));
	dict.Insert(make_pair("string", "趼睫揹"));
	dict.Insert(make_pair("map", "華芞"));
	dict["left"];
	dict["left"] = "酘晚";
	dict["map"] = "華芞﹜茬扞";

	auto it = dict.begin();

	while (it != dict.end())
	{
		cout << it->first << ":" << it->second << endl;
		++it;
	}
	cout << endl;
}